
#include <cstdio>
#include <BPainter>
#include <BReadWrite>
#include <BImage>
#include <BImage>
#include <BLineEdit>
#include <BButton>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BFont>
#include <BDragEvent>
#include <BDrag>
#include <BStyle>
#include <BFolder>
#include <BApplication>
#include <BMessageBox>

#include "LoginDialog.h"

LoginDialog::LoginDialog(BWidget* parent) : BDialog(parent)
{
	this->setBorder(5);

	_logoLabel = new BLabel();
	_logoLabel->setName("logo");
	_logoLabel->setSizePolicy(Policy_Expanding, Policy_Expanding);
	_logoLabel->setAlign(Align_Center);
	_logoLabel->setMinSize(200);

	BGridLayout* gridLayout = new BGridLayout();
	{
		BLabel* accountLabel = new BLabel("Account", this);
		accountLabel->setBorder(1);
		_usernameEdit = new BLineEdit(this);
		_usernameEdit->setBorder(1);

		BLabel* passwordLabel = new BLabel("Password", this);
		passwordLabel->setBorder(1);
		_passwordEdit = new BLineEdit(this);
		_passwordEdit->setBorder(1);

		gridLayout->setMargin(1);
		gridLayout->setWidget(0, 0, accountLabel);
		gridLayout->setWidget(0, 1, _usernameEdit);
		gridLayout->setWidget(1, 0, passwordLabel);
		gridLayout->setWidget(1, 1, _passwordEdit);
		gridLayout->setColumnStretch(1, 10);
	}

	BHBoxLayout* buttonLayout = new BHBoxLayout();
	{
		_loginButton = new BButton("Login", this);
		_loginButton->setEnabled(false);

		_quitButton = new BButton("Quit", this);

		buttonLayout->setSizePolicy(Policy_Expanding, Policy_Preferred);
		buttonLayout->addSpring();
		buttonLayout->addWidget(_loginButton);
		buttonLayout->addWidget(_quitButton);
	}

	this->setLayout(buttonLayout, Part_Bottom);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->setMargin(1);
	layout->addWidget(_logoLabel, 100);
	layout->addLayout(gridLayout, 0);

	connect(_usernameEdit, Signal_TextEdited, &LoginDialog::slotUsernameOrPasswordEdited);
	connect(_passwordEdit, Signal_TextEdited, &LoginDialog::slotUsernameOrPasswordEdited);
	connect(_loginButton, Signal_Clicked, &LoginDialog::slotLoginClicked);
	connect(_quitButton, Signal_Clicked, &LoginDialog::slotQuitClicked);
}
LoginDialog::~LoginDialog()
{

}

void LoginDialog::setSocket(BTcpSocket* socket)
{
	_socket = socket;
}
BTcpSocket* LoginDialog::socket() const
{
	return _socket;
}

void LoginDialog::replyLogin(const BBuffer& buffer)
{
	this->hide();
}
void LoginDialog::replyLogout(const BBuffer& buffer)
{
	_passwordEdit->setText(BString());
}

void LoginDialog::slotUsernameOrPasswordEdited()
{
	const BString& account = _usernameEdit->text();
	const BString& password = _passwordEdit->text();
	if (account.size() >= 3 && password.size() >= 6)
		_loginButton->setEnabled(true);
	else
		_loginButton->setEnabled(false);
}

void LoginDialog::slotLoginClicked(BObject* object, const BValue& value)
{
	if (_socket->state() == BSocket::State_Connected)
	{
		const BString& account = _usernameEdit->text();
		const BString& password = _passwordEdit->text();
		BString loginText("login{");
		loginText << "username=" << account << ';';
		loginText << "password =" << password << '}';
		_socket->send(loginText);
	}
}
void LoginDialog::slotQuitClicked(BObject* object, const BValue& vlalue)
{
	disconnect(_socket);
	BApplication::done();
}

void LoginDialog::slotConnected(BObject* object, const BValue& value)
{
	_usernameEdit->setEnabled(true);
	_passwordEdit->setEnabled(true);
}
void LoginDialog::slotReadyRead(BObject* object, const BValue& vlalue)
{
	int ret = _socket->recv(_buffer);
	while (_buffer.size() >= sizeof(int) * 3)
	{
		//int protocol = Package::GetProtocol(_buffer);
		//int size = Package::GetSize(_buffer);
		//if (protocol > 0)
		//{
		//	if (_quests[protocol])
		//	{
		//		(this->*_quests[protocol])(_buffer);
		//	}
		//}
		//if (protocol < 0)
		//{
		//	if (_replys[-protocol])
		//	{
		//		(this->*_replys[-protocol])(_buffer);
		//	}
		//}
		//_buffer.seek(0);
		//_buffer.remove(size);
	}
}
void LoginDialog::slotDisConnected(BObject* object, const BValue& value)
{
	_usernameEdit->setEnabled(false);
	_passwordEdit->setEnabled(false);
	_loginButton->setEnabled(false);
}

void LoginDialog::showEvent(const BEvent& event)
{
	if (_socket)
	{
		connect(_socket, Signal_Connected, &LoginDialog::slotConnected);
		connect(_socket, Signal_ReadyRead, &LoginDialog::slotReadyRead);
		connect(_socket, Signal_Disconnected, &LoginDialog::slotDisConnected);
	}
	_usernameEdit->setFocused(true);
	BDialog::showEvent(event);
}
void LoginDialog::hideEvent(const BEvent& event)
{
	if (_socket)
	{
		disconnect(_socket);
	}
	BDialog::hideEvent(event);
}

void LoginDialog::styleEvent(const BEvent& event)
{
	BDialog::styleEvent(event);
	const BStyle* style = this->realStyle();
	if (style)
	{
		if (BImage* image = style->image("logo"))
		{
			_logoLabel->setImage(image);
			_logoLabel->setImageSize(image->size());
		}
	}
}
void LoginDialog::updateEvent(const BEvent& event)
{
	if (!_socket)
		return;
	if (this->visible() && _socket->state() == BSocket::State_Connected)
	{

	}
}

void LoginDialog::paintEvent(const BEvent& event)
{
	BDialog::paintEvent(event);
}

void LoginDialog::dragDropEvent(const BDragEvent& dragEvent)
{
	//if (dragEvent.dragType() == Drag_Files)
	{
		BDrag* drag = dragEvent.drag();
		const BString& text = drag->text();
		BStringList fileNames = text.split(';');
		BLabel* label = (BLabel*)this->find("icon");
		if (fileNames.size() == 1 && label)
		{
			BImage* image = ReadFile<BImage>(fileNames[0]);
			label->setImage(image);
		}
	}
}

